public class Main {
    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.searchInsert(new int[]{1,3,5,6}, 5));
        System.out.println(s.searchInsert(new int[]{1,3,5,6}, 2));
        System.out.println(s.searchInsert(new int[]{1,3,5,6}, 7));
    }
}

//二刷
class Solution {
    public int searchInsert(int[] nums, int target) {
        int l = 0, h = nums.length - 1;

        while (l <= h) {
            int mid = (l + h) / 2;
            if (nums[mid] < target) {
                l = mid + 1;
            } else {
                h = mid - 1;
            }
        }

        return l;
    }
}


//class Solution {
//    public int searchInsert(int[] nums, int target) {
//        return biSearch(nums, target);
//    }
//
//    int biSearch(int[] nums, int target){
//        int l = 0, h = nums.length - 1;
//        int ans = nums.length;
//        while(l <= h){
//            int mid = (l + h) / 2;
//            if(nums[mid] > target){
//                ans = mid;
//                h = mid - 1;
//            }
//            else if(nums[mid] < target){
//                l = mid + 1;
//            }
//            else{
//                ans = mid;
//                break;
//            }
//        }
//        return ans;
//    }
//}